import { MaxPercentNum } from "../配置/基础配置";
import { checkActor } from "./mirUtil";

//计算2个目标之间的距离
export function getDistance(act1: TActor, act2: TActor): number {
    let dis = 10 ^ 10;
    if (checkActor(act1) && checkActor(act2)) {
        if (act1.GetMap().GetMapID() === act2.Map.MapID) {
            let x = Math.abs(act1.MapX - act2.MapX)
            let y = Math.abs(act1.MapY - act2.MapY)
            let jl = Math.max(x, y)
            return jl;
        }
    }
    return dis;
}


//随时数 如果只穿一个参数 随机 0- min
export function randomInRange(min: number, max?: number): number {
    if (!max) {
        max = min;
        min = 0;
    }
    if (min > max) {
        let t = min;
        min = max;
        max = t;
    }

    let range = max - min;
    if (range == 0) {
        return min;
    }
    return Math.floor((Math.random() * (range) + min));
}



export function isNull(value) {
    return value === null || value === undefined;
}

export function getValue(value, defaultValue) {
    if (isNull(value)) {
        return defaultValue;
    }
    return value;
}

//生成一个0-num-1的随机数组
export function getRandomListByNum(num: number): Array<number> {
    let list = [];
    if (num > 0) {
        for (let i = 0; i < num; i++) {
            list.push(i);
        }
        return randomList(list);
    }
    return list;
}

//将一个数组进行随机排序(生成新数组)
export function getRandomList(list: Array<number>): Array<number> {
    let newList = [];
    if (list && list.length > 0) {
        for (let i = 0; i < list.length; i++) {
            newList.push(list[i]);
        }
    }
    return randomList(newList);
}

function randomList(list: Array<number>): Array<number> {
    if (list && list.length > 0) {
        for (let i = 0; i < list.length; i++) {
            let index = randomInRange(i, list.length);
            let tmp: number = list[i];
            list[i] = list[index];
            list[index] = tmp;
        }
    }
    return list
}

//伤害吸收转换  由值转成百分比
export function changeDamageCut(percent) {
    percent = percent || 0
    return percent / (percent + MaxPercentNum);
}

export function getArrayItem(array: Array<any>, index): any {
    if (!array || array.length == 0) {
        return null
    }
    if (index >= array.length) {
        return array[array.length - 1];
    }
    else {
        return array[index]
    }
}